/*
 * @(#)XFileFilter.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.util;

import javax.swing.filechooser.*;
import java.io.File;
import java.util.Vector;

/**
 * Filtros de la ventana de selecci�n de ficheros
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XFileFilter extends FileFilter {

	//----------------------------------------------------------------------------//
	//                           CONSTANTES P�BLICAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * Selecci�n de todos los ficheros
	 */
	public static final int ALLFILES = 0;
	
	/**
	 * Selecci�n de directorios
	 */
	public static final int NONEFILE = 1;
	
	/**
	 * Selecci�n configurable
	 */
	public static final int USERFILES = 2;
	
	/**
	 * Varias terminaciones
	 */
	public static final int USERSOMEFILES = 3;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Tipo de filtro
	 */
	private int code;
	
	/**
	 * Terminaci�n de los ficheros a mostrar
	 */
	private String ending;
	
	/**
	 * Terminaciones permitidas
	 */
	private Vector<String> endings;
	
	/**
	 * Descripci�n del filtro
	 */
	private String description;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor de un filtro predise�ado
	 */
	public XFileFilter(int code) {
		this.code = code;
		this.ending = "";
		this.endings = null;
		if(code == ALLFILES) this.description = "All files";
		else if(code == NONEFILE) this.description = "Directories";
		else this.description = "";
	}

	/**
	 * Constructor de un filtro creado por el usuario
	 * 
	 * @param ending
	 * @param description
	 */
	public XFileFilter(String ending, String description) {
		this.code = USERFILES;
		this.ending = ending;
		this.endings = null;
		this.description = description;
	}

	/**
	 * Constructor de un filtro creado por el usuario
	 * 
	 * @param ending
	 * @param description
	 */
	public XFileFilter(Vector<String> endings, String description) {
		this.code = USERSOMEFILES;
		this.ending = null;
		this.endings = endings;
		this.description = description;
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Estudia si el fichero es filtrado
	 */
	public boolean accept(File f) {
		if(code == ALLFILES) return true;
		if(code == NONEFILE) return false;
		if(code == USERFILES && f.getName().endsWith(ending)) return true;
		if(code == USERSOMEFILES) {
			String fname = f.getName();
			int size = endings.size();
			for(int i=0; i<size; i++) {
				if(fname.endsWith(endings.elementAt(i))) return true;
			}
		}
		if(f.isDirectory()) return true;
		return false;
	}

	/**
	 * Descripci�n del filtro
	 */
	public String getDescription() {
		return description;
	}
}

